# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.12.13.0
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-partio"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-clang-15"
             "${MINGW_PACKAGE_PREFIX}-llvm-15"
             $([[ ${CARCH} != i686 ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-qt6-base" || echo \
               "${MINGW_PACKAGE_PREFIX}-qt5-base"))
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python module")
if [[ ${CARCH} != i686 ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt6-base: for osltoy")
else
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt5-base: for osltoy")
fi
source=(${_realname}-${pkgver}.tar.gz::https://github.com/imageworks/OpenShadingLanguage/archive/v${pkgver}.tar.gz
        0001-dont-add-fno-rtti-compile-option.patch
        0002-dont-add-flex-include-dir.patch
        0010-macros-undef-mingw.patch
        0012-isatty-mingw.patch
        0013-disable-assert-partio.patch
        0014-fix-testshade-library-name.patch
        0015-llvm-linking.patch)
sha256sums=('b5fc9f4814c1ec927773b2236d126072c1d0bbc0f031841b82649ff4d29e9d7e'
            '36d7d16bcf9dedebd254c24fa2f4f70ef5bdf4cea8beee380931cfe5b8c45d7d'
            '9a0b5597c6047917b9541192e0f71b28dcfd17ea3a5948892f68d38b5e1b4946'
            '8b42553133e487077363a96520131f84453a81afdd9d198ef6827e0395ff1208'
            'c7c125e6fed4650f9770d92ea24771e5518b344718dde291a6d5d4ad945c6222'
            '5277727f3d03ea88f407c1092c28519f58772e89518487da7c55f1d24c1f3c79'
            '3146942267540de2a3146ef8222d59fdb265dc8fffa3f3a2207bbc851fc99ab8'
            '1b2c1e7faae162078696049f63c2b55e8c111ffeff25abaa49d67877c81a71d1')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${srcdir}/OpenShadingLanguage-${pkgver}

  apply_patch_with_msg \
    0001-dont-add-fno-rtti-compile-option.patch \
    0002-dont-add-flex-include-dir.patch \
    0010-macros-undef-mingw.patch \
    0012-isatty-mingw.patch \
    0013-disable-assert-partio.patch \
    0014-fix-testshade-library-name.patch \
    0015-llvm-linking.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}/opt/llvm-15
  CFLAGS+=" -Wno-attributes"
  CXXFLAGS+=" -Wno-attributes -DTINYFORMAT_ALLOW_WCHAR_STRINGS"

  local _sse_arch=""
  if [[ ${CARCH} == x86_64 ]]; then
    _sse_arch="sse3"
  elif [[ ${CARCH} == i686 ]]; then
    _sse_arch="sse2"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DOSL_BUILD_TESTS=OFF \
    -DSTOP_ON_WARNING=OFF \
    -DLLVM_STATIC=ON \
    -DLLVM_ROOT=${MINGW_PREFIX}/opt/llvm-15 \
    -DUSE_SIMD=${_sse_arch} \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ${extra_config} \
    ../OpenShadingLanguage-${pkgver}

  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
  # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe.
  PATH=${srcdir}/build-${MSYSTEM}/src/liboslcomp:"${PATH}" ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}//build-${MSYSTEM}

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}/OpenShadingLanguage-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/OSL/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
